<!doctype html>
<html>
<head>
    <title>sign-here demo</title>
    <meta name="format-detection" content="telephone=yes">
    <meta name="viewport" content="width=device-width, maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="/static/ajz/js/jquery.min.js" ></script>
    <style type="text/css">

        html,body{
            width: 100%;
            height: 100%;
            background-color:#FFF;
        }
        p{
            margin:0px;
            padding:0px;
        }
        @media only screen and (min-width: 100px) and (max-width:330px){
            #clearCanvas{
                width: 99.9%;
                height: 40px;
                line-height: 40px;
                text-align: center;
                float: left;
                border: 1px solid #DEDEDE;
                position: fixed;
                left: -39%;
                top: 4.4%;
                transform:rotate(90deg);
                -ms-transform:rotate(90deg);
                -moz-transform:rotate(90deg);
                -webkit-transform:rotate(90deg);
                -o-transform:rotate(90deg);
            }
        }
        #canvas{
            width: 100%;
            height: 100vh;
            float: left;
            touch-action: none;
            -o-touch-action: none;
            -ms-touch-action: none;
            -moz-touch-action: none;
            -webkit-touch-action: none;
            /*移动端 触摸时 不允许用户进行操作 （也就是禁用滑动，缩放页面和元素等）*/
        }
        #canvas canvas{

            width: 100%;
            height: 100vh;
        }
        #clearCanvas{
            width: 99.9%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            float: left;
            border: 1px solid #DEDEDE;
            position: fixed;
            left: -44%;
            top: 50%;
            transform:rotate(90deg);
            -ms-transform:rotate(90deg);
            -moz-transform:rotate(90deg);
            -webkit-transform:rotate(90deg);
            -o-transform:rotate(90deg);
        }
        #saveCanvas{
            width:48%;
            height: 40px;
            line-height: 40px;
            text-align: center;
            /*float: left;*/
            border: 1px solid #DEDEDE;
            position: absolute;
            left:49%;
            bottom:0px;
        }
    </style>
</head>
<body>
<form action="" method="post" name="" id="" enctype="multipart/form-data">
    <div id="canvas">
        <p id="clearCanvas">清除</p>
        <input type="button" value="确定" style="display:none" id="saveCanvas">
    </div>
    <input type="hidden" name="canvas_user_hiddem" value="" id="canvas_user_hiddem">
</form>
<script type="text/javascript">
    window.onload = function() {
        new lineCanvas({
            el: document.getElementById("canvas"),//绘制canvas的父级div
            clearEl: document.getElementById("clearCanvas"),//清除按钮
            saveEl: document.getElementById("saveCanvas"),//保存按钮
            //      linewidth:1,//线条粗细，选填
            //      color:"black",//线条颜色，选填
            //      background:"#ffffff"//线条背景，选填
        });
    };
    function lineCanvas(obj) {
        this.linewidth = 1;
        this.color = "#000000";
        this.background = "transparent";
        for (var i in obj) {
            this[i] = obj[i];
        };
        this.canvas = document.createElement("canvas");
        this.el.appendChild(this.canvas);
        this.cxt = this.canvas.getContext("2d");
        this.canvas.width = this.el.clientWidth;
        this.canvas.height = this.el.clientHeight;
        this.cxt.fillStyle = this.background;
        this.cxt.fillRect(0, 0, this.canvas.width, this.canvas.width);
        this.cxt.strokeStyle = this.color;
        this.cxt.lineWidth = this.linewidth;
        this.cxt.lineCap = "round";
        //开始绘制
        this.canvas.addEventListener("touchstart", function(e) {
            this.cxt.beginPath();
            this.cxt.moveTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
        }.bind(this), false);
        //绘制中
        this.canvas.addEventListener("touchmove", function(e) {
            this.cxt.lineTo(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
            this.cxt.stroke();
        }.bind(this), false);
        //结束绘制
        this.canvas.addEventListener("touchend", function() {
            this.cxt.closePath();
            //给父窗口中的元素赋值
            var v = window.parent.document.getElementById("hotelDzqm");
            var c = window.parent.document.getElementById("mycanvar");
            var $v = $(v);
            var $c = $(c);
            var imgBase64 = this.canvas.toDataURL();
            $v.val(imgBase64);
            $c.attr("src",imgBase64);
        }.bind(this), false);
        //清除画布
        this.clearEl.addEventListener("click", function() {
            this.cxt.clearRect(0, 0, this.canvas.width, this.canvas.height);
        }.bind(this), false);
        //保存图片，直接转base64
        this.saveEl.addEventListener("click", function() {
            var imgBase64 = this.canvas.toDataURL();
            console.log(imgBase64);
            $('#canvas_user_hiddem').val(imgBase64);
            // alert($('#canvas_user_hiddem').val())

        }.bind(this), false);
    };
    // alert(window.name)
    // alert($('#canvas_user_hiddem').val())
</script>
</body>
</html>